function [ROI, timestep, numReg, ROInumber, ROIfigure] = GetARGUSrois(IntensityImageData, ARGUS_Filename, PlotAllROIs)

% This will load the ARGUS Flow Calculator ROI values for each phase time.
% Coordinates are exported in units of pixels

% *INPUT*
% IntensityImageData: include this if you want to overlay ROI's on an image
% ARGUS_Filename    : self explanatory
% PlotALLROIs       : '1' if you want all ROIs, '0' if you want to select one

% *OUTPUT*
% ROI      : ROI coodinates - ROI(coordinate,'x' or 'y',timestep,region number)
% timestep : number of timesteps
% numReg   : number of regions
% ROInumber: Region to evaluate
% ROIfigure: Figure handle

% AJB, 12/23/06

IntensityPlot = 1;

if nargin < 1
    [FileName, PathName] = uigetfile('*.txt','Select ARGUS textfile');
    IntensityPlot = 0;
    PlotAllROIs = 1;
    if isequal(FileName,0)
        beep
        disp('User selected Cancel')
        return
    end
    ARGUS_Filename = [PathName '/' FileName];
end    
    
timestep   = 0;
anotherIm  = 1;
anotherReg = 1;
ROI = 0;

% Load Argus data
fid = fopen(ARGUS_Filename);

%% Search for, and find the number of images and the number of Regions
while anotherReg

    tline = fgetl(fid);
    
    if strfind(tline,' Img    TT   RefVel')
        
        %skip some lines once the tag 'Img    TT   RefVel' is found 
        tline    = fgetl(fid);
        tline    = fgetl(fid);

        % Find the total number of images in the scan sequence
        while anotherIm
            
            timestep = str2double(tline(1:4));
            tline    = fgetl(fid);
            if isempty(tline)
                anotherIm = 0;
            end
            
        end
        
        for i = 1:6
            tline = fgetl(fid);
        end  % skip 6 lines
        
        % count the number of regions
        while anotherReg
            
            numReg = str2double(tline(1:4));
            tline   = fgetl(fid);
            if isempty(tline)
                anotherReg = 0;
            end
            
        end
    
    end

end

%% Find flow contours tag
while 1

    tline = fgetl(fid);
    if strfind(tline,'Flow contours:'),break, end
        
end

tline = fgetl(fid); % start at the first region coordinates
tline = fgetl(fid);

% StartIndex = find(tline == '(');
% EndIndex   = find(tline == ')');
% CommaIndex = find(tline == ',');

for i=1:timestep
    
    for j = 1:numReg
        
        StartIndex = find(tline == '(');
        EndIndex   = find(tline == ')');
        CommaIndex = find(tline == ',');
        
        for k = 1:1:length(StartIndex)
           
            % Get current point string - Comma Delimited            
            ROI(k,2,i,j) = str2double(tline(StartIndex(k)+1:CommaIndex(k)-1))+1;
            ROI(k,1,i,j) = str2double(tline(CommaIndex(k)+1:EndIndex(k)-1))+1;
             
        end
        
        tline = fgetl(fid);
        
    end
    
    tline = fgetl(fid);
    tline = fgetl(fid);
       
end

ROIfigure  = figure('Position', [0 400 300 300], 'Color',[1 1 1], 'Name','ROI''s');
haxes      = axes;

Xmin = min(nonzeros(ROI(:,1,:)))-5; % 5 pixels zoom out...
Xmax = max(nonzeros(ROI(:,1,:)))+5;
Ymin = min(nonzeros(ROI(:,2,:)))-5;
Ymax = max(nonzeros(ROI(:,2,:)))+5;

if IntensityPlot == 1
    imagesc(IntensityImageData);
    colormap(gray);
    set(get(haxes,'Title'),'String','ARGUS determined ROI''s','FontSize',12);
    axis ij; axis equal; hold on; axis manual
    axis([Xmin Xmax Ymin Ymax]);


    for i = 1:timestep %changed for debug need to comment out for animation


        for j = 1:numReg

            ROImin = min(nonzeros(ROI(:,1,i,j)));

            if isempty(ROImin)
                ROImin = -100;
            end

            plot(nonzeros(ROI(:,1,i,j)),nonzeros(ROI(:,2,i,j)),'g','LineWidth',2)
            text(ROImin,ROI(1,2,i,j),['Region = ' num2str(j,1)], 'Color', [1 0 0], 'FontSize',12);
        end

        %     D(i) = getframe;
    end
end


if (numReg > 1) & ~(PlotAllROIs)
    ROInumber = str2num(cell2mat(inputdlg('Enter Region to evaluate', 'Region Selection', 1, {'1'})));
    close
else
    ROInumber = 1;
end

hold off

% movie2avi(D, 'Aorta Wall', 'compression','Cinepak');
% close
fclose(fid);